$(function(){
	$("input[name='relevanceStatus'],[name='paginationStatus']").each(function(){
		
		$(this).dblclick(function(){
			$(this).removeAttr("checked");
		});
		
	});
	var currentDate = (new Date()).Format("yyyy-MM-dd");
	$("input[name='uploadStartDate'],[name='uploadEndDate']").val(currentDate);
});

/*点击查询按钮*/
function queryBtnClick(){
	$("input[name='currentPage']").val(1);
	queryFormSubmit();
}

/*提交查询*/
function queryFormSubmit(){
	var submitData = $('#queryForm').serialize();
	var basePath = $("#basePath").val();
	clearPage();
	layer.load(1,{offset: '200px'});
	$.ajax({
		url: basePath + "/page/ajax_query_uploadify",
		dataType: "json",
		data:submitData,
		success: function( data ) {
			var success = data['success'];
			if(success){
				
				var params = data['data'];
				var totalCounts = params['totalCounts'];
				if(totalCounts > 0){
					layer.msg("查询成功",{offset: '200px'});
					var records = params['datas'];
					addRows(records);
					var pageSize = params['pageSize'];
					var currentPage = params['currentPage'];
					initPage(parseInt(totalCounts),parseInt(pageSize),parseInt(currentPage));
					
				}else{
					layer.msg("查询结果为空",{offset: '200px'});
				}
				
			}else{
				layer.msg("查询失败",{offset: '200px'});
			}
			console.log(data);
			
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
			if(XMLHttpRequest.status == 200){
				if(textStatus == 'parsererror'){
					layer.msg("登陆已过期，请重新登陆",{offset: '200px'});
				}else{
					layer.msg("未知错误，请联系管理员",{offset: '200px'});
				}
			}else if(XMLHttpRequest.status == 0){
				if(textStatus == 'error'){
					layer.msg("无法连接服务器，请检测网络",{offset: '200px'});
				}else{
					layer.msg("未知错误，请联系管理员",{offset: '200px'});
				}
			}else{
				layer.msg("未知错误，请联系管理员");
			}
		},
		complete:function(XMLHttpRequest, textStatus){
			layer.closeAll('loading');
		}
	});
	
};

/*添加列表行*/
function addRows(records){
	var basePath = $("#basePath").val();
	for(var index in records){
		var record = records[index];
		var add_content=$("#template_tr").clone();
		add_content.removeAttr("hidden");
		add_content.attr("id",record.id);
		add_content.find("span[class='original_folder']").html(record.original_folder);
		add_content.find("span[class='file_count']").html(record.count);
		add_content.find("span[class='upload_date']").html(record.upload_date_format);
		add_content.find("span[class='user_code']").html(record.user_code);
		if(record.visit_guid){
			add_content.find("span[class='relevance_status_name']").html("已关联");
			add_content.find("td[data-type='operate']").html("<a target='_blank' href='"+basePath+"/medrecord/paginationMedrecordImages?firstGuid="+record.visit_guid+"' title='病案编页'>病案编页&nbsp;</a> ");
		}else{
			add_content.find("span[class='relevance_status_name']").html("未关联");
		}
		
		if(record.pagination_visit_guid){
			add_content.find("span[class='pagination_status_name']").html("已编页");
		}else{
			add_content.find("span[class='pagination_status_name']").html("未编页");
		}
		
		/*add_content.find("span[class='file_count']").html(record.file_count);*/
		
		
		
		$("#query_show_table tbody").append(add_content);
    } 
	
}

/*初始化分页控件*/
function initPage(totalCounts,pageSize,currentPage){
	 var visiblePages = 5;
	 var totalPages =  totalCounts%pageSize==0?(totalCounts/pageSize):(parseInt(totalCounts/pageSize)+1);
	 if(totalCounts < 1){
		$("#page_plus").html("");
		$("#totalPage").html("0");
        $("#currentPage").html("0/0");
	 }else{
		 $('#page_plus').jqPaginator({

	        totalCounts: totalCounts,
	        pageSize:pageSize,
	        visiblePages: visiblePages,
	        currentPage: currentPage,

	        first:'<li class="first"><a href="javascript:void(0);">首页</a></li>',
	        prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
	        next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
	        last: '<li class="last"><a href="javascript:void(0);">末页</a></li>',
	        page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
	        onPageChange: function (num,type) {

	     		if(type !='init'){
	     			$("input[name='currentPage']").val(num);
	     			queryFormSubmit();
				}
	            $("#totalPage").html(totalCounts);
	            $("#currentPage").html(num+"/"+totalPages);
	        }
	    });
	 }
}

/*清空列表和分页控件*/
function clearPage(){
	$("#template_tr").siblings("tr").remove();
	$("#page_plus").html("");
	$("#totalPage").html("0");
    $("#currentPage").html("0/0");
}


Date.prototype.Format = function(fmt) { //author: meizz 
	var o = {
		"M+" : this.getMonth() + 1, //月份 
		"d+" : this.getDate(), //日 
		"h+" : this.getHours(), //小时 
		"m+" : this.getMinutes(), //分 
		"s+" : this.getSeconds(), //秒 
		"q+" : Math.floor((this.getMonth() + 3) / 3), //季度 
		"S" : this.getMilliseconds() //毫秒 
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}